package com.sqrj.phoneorder.service;

import com.sqrj.phoneorder.Vo.*;
import com.sqrj.phoneorder.dao.SendTruckOUTMapper;
import com.sqrj.phoneorder.dto.SendCountDto;
import com.sqrj.phoneorder.model.SendChart;
import com.sqrj.phoneorder.model.SendTruckOUT;
import com.sqrj.phoneorder.param.SendCountParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SendTruckOUTService {
    @Resource
    private SendTruckOUTMapper sendTruckOUTMapper;

//  初始化对应等级名称
    static String[] levels = {"C10","C15","C20","C25","C30","C35","C40","C45","C50","C55","C60","Other"};
//  转换为等级list集合
    static List<String> list = Arrays.asList(levels);
//  初始化对应生产线名称
    static String[] proLine = {"1","2","3","4","5","Other"};
//  转换为生产线list集合
    static List<String> linelist = Arrays.asList(proLine);

//    static List<SendProLineVo> staticsendProLineVoList = new ArrayList<>();
//
//    static {
//        for(int i = 1 ;i <= 5; i++){
//            staticsendProLineVoList.add(new  SendProLineVo(""+i,0.0,"/images/"+i+"line.png"));
//        }
//    };

//    static List<SendLevelVo> staticsendLevelVoList = new ArrayList<>();
//
//    static {
//        for(int i = 10 ;i <= 60; i+=5){
//            staticsendLevelVoList.add(new  SendLevelVo("C"+i,0.0,0,"/images/C"+i+".png"));
//        }
//    }

//    根据单位查询发货单记录
    public List<SendTruckOUT> querystSendRecord(String unitName) {
        return sendTruckOUTMapper.querystSendRecord(unitName);
    }
//  根据单位查询发货单汇总
    public List<SendTruckOUT> querystSendHZ(String unitName) {
        return sendTruckOUTMapper.querystSendHZ(unitName);
    }
//  根据单位查询发货图表数据
    public List<SendChart> querySendChart(String unitName) {
        return sendTruckOUTMapper.querySendChart(unitName);
    }

    //按客户
    public List<SendCustomerVo> getSendCountByCustomer(SendCountParam sendCountParam) {

        System.out.println("sendCountParam"+sendCountParam);
        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        System.out.println("sendCountDto"+sendCountDto);

        List<SendCustomerVo> sendCustomerVoList = sendTruckOUTMapper.getPageListBysendCountDto(sendCountDto);

        return sendCustomerVoList;
    }


    //按等级
    public List<SendLevelVo> getSendCountByLevel(SendCountParam sendCountParam) {

        List<SendLevelVo> staticsendLevelVoList = new ArrayList<>();

        for(int i = 10 ;i <= 60; i+=5){
            staticsendLevelVoList.add(new  SendLevelVo("C"+i,0.0,0,"/images/C"+i+".png"));
        }


        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<SendLevelVo> sendLevelVoList = sendTruckOUTMapper.getPageListByLevel(sendCountDto);

        SendLevelVo staticSendLevelVo = new SendLevelVo("Other",0.0,0,"/images/other.png");

            if(!staticsendLevelVoList.get(staticsendLevelVoList.size()-1).getConlevel().equals("Other")) {
                staticsendLevelVoList.add(staticSendLevelVo);
            }else{
                staticsendLevelVoList.remove(staticsendLevelVoList.size()-1);
                staticsendLevelVoList.add(staticSendLevelVo);
            }

            for(int i= 0;i<sendLevelVoList.size();i++){
                SendLevelVo sendLevelVo = sendLevelVoList.get(i);
                if(list.indexOf(sendLevelVo.getConlevel())>-1){
                    int index = list.indexOf(sendLevelVo.getConlevel());
                    sendLevelVo.setImgpath(staticsendLevelVoList.get(index).getImgpath());
                    Collections.replaceAll(staticsendLevelVoList,staticsendLevelVoList.get(index),sendLevelVo);
                }else {
                    staticSendLevelVo.setConnum(staticSendLevelVo.getConnum()+sendLevelVo.getConnum());
                    staticSendLevelVo.setSumsendnub(staticSendLevelVo.getSumsendnub()+sendLevelVo.getSumsendnub());
                    Collections.replaceAll(staticsendLevelVoList,staticSendLevelVo,staticSendLevelVo);
                }

            }

            return staticsendLevelVoList;

    }


    //按生产线
    public List<SendProLineVo> getSendCountByProLine(SendCountParam sendCountParam) {

            SendCountDto sendCountDto = getSendCDto(sendCountParam);

            List<SendProLineVo> staticsendProLineVoList = new ArrayList<>();


            for(int i = 1 ;i <= 5; i++){
                staticsendProLineVoList.add(new  SendProLineVo(""+i,0.0,"/images/"+i+"line.png"));
            }

            List<SendProLineVo> sendProLineVoList = sendTruckOUTMapper.getPageListByProLine(sendCountDto);
            SendProLineVo staticSendProLineVo = new SendProLineVo("Other",0.0,"/images/other.png");
            //
            if(!staticsendProLineVoList.get(staticsendProLineVoList.size()-1).getProductlineid().equals("Other")) {
                staticsendProLineVoList.add(staticSendProLineVo);
            }else{
                staticsendProLineVoList.remove(staticsendProLineVoList.size()-1);
                staticsendProLineVoList.add(staticSendProLineVo);
            }

            System.out.println("sendProLineVo----------测试"+sendProLineVoList.size());

            for(int i= 0;i<sendProLineVoList.size();i++){
                SendProLineVo sendProLineVo = sendProLineVoList.get(i);
                System.out.println("sendProLineVo----------测试"+sendCountDto);
                //
                if(linelist.indexOf(sendProLineVo.getProductlineid())>-1){
                    int index = linelist.indexOf(sendProLineVo.getProductlineid());
                    sendProLineVo.setImgpath(staticsendProLineVoList.get(index).getImgpath());
                    Collections.replaceAll(staticsendProLineVoList,staticsendProLineVoList.get(index),sendProLineVo);
                }else {
                    staticSendProLineVo.setConnum(staticSendProLineVo.getConnum()+sendProLineVo.getConnum());
                    Collections.replaceAll(staticsendProLineVoList,staticSendProLineVo,staticSendProLineVo);
                }

            }

            return staticsendProLineVoList;


    }


    //转换接收参数，对时间赋默认值公共方法
    private static SendCountDto getSendCDto(SendCountParam sendCountParam){
        SendCountDto sendCountDto = new SendCountDto();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(sendCountParam.getFromTime())) {
                sendCountDto.setFromTime(dateFormat.parse(sendCountParam.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
//                cal.add(Calendar.DATE, -3);
                sendCountDto.setFromTime(cal.getTime());
            }
            if (StringUtils.isNotBlank(sendCountParam.getToTime())) {
                sendCountDto.setToTime(dateFormat.parse(sendCountParam.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                sendCountDto.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        sendCountDto.setJbzCode(sendCountParam.getJbzCode());

        if(sendCountParam.getSpecialparam() == null || sendCountParam.getSpecialparam().equals("全部")){
            System.out.println("------为空");
        }else {
            sendCountDto.setSpecialparam(sendCountParam.getSpecialparam());
        }

        return sendCountDto;
    }


    //按施工单位
    public List<SendProUnitVo> getSendCountByProUnit(SendCountParam sendCountParam) {

        System.out.println("sendCountParamunit"+sendCountParam);
        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        System.out.println("sendCountDtounit"+sendCountDto);

        List<SendProUnitVo> sendProUnitVoList =  sendTruckOUTMapper.getPageListByProUnit(sendCountDto);

        return sendProUnitVoList;
    }

    //按现场联系人
    public List<SendLinkManVo> getSendCountByLinkMan(SendCountParam sendCountParam) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        System.out.println("现场联系人"+sendCountDto);

        List<SendLinkManVo> sendLinkManVoList = sendTruckOUTMapper.getPageListByLinkMan(sendCountDto);

        return sendLinkManVoList ;
    }
//
//    //按工程名称
//    public String getSendCountByPro(SendCountParam sendCountParam, String proUnit) {
//
//        StringBuilder ProList = new StringBuilder();
//        SendCountDto sendCountDto = getSendCDto(sendCountParam);
//
//        List<String> sendProList = jblSendTruckOUTMapper.getPageListByPro(sendCountDto, proUnit);
//
//        for(int i= 0;i<sendProList.size();i++){
//            ProList.append("("+(i+1)+")"+"."+sendProList.get(i)+"   ");
//        }
//
//        return ProList.toString();
//    }
//
//    //联系电话
//    public String getSendCountByProLinkMan(SendCountParam sendCountParam, String proLinkMan) {
//
//        StringBuilder ProLinkManList = new StringBuilder();
//        SendCountDto sendCountDto = getSendCDto(sendCountParam);
//
//        List<String> sendProLinkManList = jblSendTruckOUTMapper.getPageListByProLinkMan(sendCountDto, proLinkMan);
//
//        for(int i= 0;i<sendProLinkManList.size();i++){
//            ProLinkManList.append("("+(i+1)+")"+"."+sendProLinkManList.get(i)+"   ");
//        }
//
//        return ProLinkManList.toString();
//    }


    //按车牌
    public List<SendTruckVo> getSendCountByTruck(SendCountParam sendCountParam) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<SendTruckVo> sendTruckVoList = sendTruckOUTMapper.getPageListByTruck(sendCountDto);

        return  sendTruckVoList;
    }

    //按业务员
    public List<SendSenderVo> getSendCountBySender(SendCountParam sendCountParam) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<SendSenderVo> sendSenderVoList = sendTruckOUTMapper.getPageListBySender(sendCountDto);

        return sendSenderVoList;

    }

    //按操作员
    public List<SendOperatorVo> getSendCountByOperator(SendCountParam sendCountParam) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<SendOperatorVo> sendOperatorVoList = sendTruckOUTMapper.getPageListByOperator(sendCountDto);

        return sendOperatorVoList;

    }

    //获取现场联系人集合
    public List<String> queryLinkManList(SendCountParam sendCountParam) {
        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<String> linkManList = sendTruckOUTMapper.getLinkManList(sendCountDto);

        linkManList.add(0,"全部");

        return  linkManList;

    }

    //查询客户集合
    public List<String> queryCustomerList(SendCountParam sendCountParam) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<String> customerList = sendTruckOUTMapper.getCustomerList(sendCountDto);

        customerList.add(0,"全部");

        return  customerList;
    }

    //查询操作员集合
    public List<String> queryOperatorList(SendCountParam sendCountParam) {
        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<String> operatorList = sendTruckOUTMapper.getOperatorList(sendCountDto);

        operatorList.add(0,"全部");

        return  operatorList;
    }

    //查询发货人集合
    public List<String> querySenderList(SendCountParam sendCountParam) {
        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<String> senderList = sendTruckOUTMapper.getSenderList(sendCountDto);

        senderList.add(0,"全部");

        return  senderList;
    }

    //查询车牌号集合
    public List<String> queryTruckList(SendCountParam sendCountParam) {
        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        List<String> truckList = sendTruckOUTMapper.getTruckList(sendCountDto);

        truckList.add(0,"全部");

        return  truckList;
    }

    //发货单明细
    public List<SendTruckOUT> querySendRecord(SendCountParam sendCountParam) {

        SendCountDto sendCountDto = getSendCDto(sendCountParam);

        System.out.println("发货单明细"+sendCountDto);


        return sendTruckOUTMapper.querySendRecord(sendCountDto);
    }
}
